package com.wxapp.sale.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.condition.ConditionUtil;
import com.wxapp.common.util.copy.CopyUtil;
import com.wxapp.sale.api.pojo.entity.GoodsBrandDO;
import com.wxapp.sale.api.pojo.vm.goodsBrand.PGoodsBrandDetailVm;
import com.wxapp.sale.api.pojo.vm.goodsBrand.PGoodsBrandPageVm;
import com.wxapp.sale.api.pojo.vm.goodsBrand.PGoodsBrandSelectVm;
import com.wxapp.sale.api.pojo.vo.goodsBrand.QGoodsBrandAddVo;
import com.wxapp.sale.api.pojo.vo.goodsBrand.QGoodsBrandPageVo;
import com.wxapp.sale.api.pojo.vo.goodsBrand.QGoodsBrandUpdateVo;
import com.wxapp.sale.service.mapper.GoodsBrandMapper;
import com.wxapp.sale.service.service.GoodsBrandService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lgl
 * @Description
 * @Datetime 2023/4/23 16:14
 * @Modified By
 */
@Service
@RequiredArgsConstructor
public class GoodsBrandServiceImpl extends IBaseService implements GoodsBrandService {
    private final GoodsBrandMapper goodsBrandMapper;

    @Override
    public ResultVm<String> add(UserVo userVo, QGoodsBrandAddVo qGoodsBrandAddVo) {
        LambdaQueryWrapper<GoodsBrandDO> query = new LambdaQueryWrapper<>();
        query.eq(GoodsBrandDO::getName, qGoodsBrandAddVo.getName()).eq(GoodsBrandDO::getStatus, true).eq(GoodsBrandDO::getCode, qGoodsBrandAddVo.getCode());
        var exist = goodsBrandMapper.isExist(query);
        if (exist) {
            return error("已经存在该品牌！");
        }
        var brandDO = CopyUtil.copyObjectNew(qGoodsBrandAddVo, GoodsBrandDO::new, (s, r) -> {
            r.setCreateBy(userVo.getId());
            return null;
        });
        goodsBrandMapper.insert(brandDO);
        return success("保存成功");
    }

    @Override
    public ResultVm<PGoodsBrandDetailVm> detail(String id) {
        var goodsBrand = goodsBrandMapper.selectById(id);
        if (goodsBrand == null) {
            return error("不存在该商品品牌");
        }
        return success(CopyUtil.copyObjectNew(goodsBrand, PGoodsBrandDetailVm::new, null));
    }

    @Override
    public ResultVm<String> update(UserVo userVo, QGoodsBrandUpdateVo qGoodsBrandUpdateVo) {
        var goodsBrand = goodsBrandMapper.selectById(qGoodsBrandUpdateVo.getId());
        if (goodsBrand == null) {
            return error("不存在该商品品牌");
        }
        CopyUtil.copyObject(qGoodsBrandUpdateVo, goodsBrand, (s, r) -> {
            r.setUpdateBy(userVo.getId());
            return null;
        });
        goodsBrandMapper.updateById(goodsBrand);
        return success();
    }

    @Override
    public ResultVm<String> delete(UserVo userVo, String id) {
        var goodsBrand = goodsBrandMapper.selectById(id);
        if (goodsBrand == null) {
            return error("不存在该商品品牌");
        }
        goodsBrand.setStatus(false);
        goodsBrandMapper.updateById(goodsBrand);
        return success();
    }

    @Override
    public ResultVm<Page<PGoodsBrandPageVm>> page(QGoodsBrandPageVo qListVo, UserVo userVo) {
        Map<SFunction<GoodsBrandDO, ?>, SqlKeyword> condition = new HashMap<>();
        condition.put(GoodsBrandDO::getName, SqlKeyword.LIKE);
        var selectCondition = ConditionUtil.getSCondition(GoodsBrandDO::new, qListVo, null, condition,userVo.getId());
        var page = ConditionUtil.getPage(qListVo, GoodsBrandDO.class);
        var goodsBrandPage = goodsBrandMapper.selectPage(page, selectCondition);
        return success(CopyUtil.copyPage(goodsBrandPage, PGoodsBrandPageVm::new, null));
    }

    @Override
    public ResultVm<List<PGoodsBrandSelectVm>> select(String name, UserVo userVo) {
        LambdaQueryWrapper<GoodsBrandDO> query = new LambdaQueryWrapper<>();
        query.like(CheckUtil.isNotNullString(name),GoodsBrandDO::getName, name).eq(GoodsBrandDO::getStatus, true)
                .select(GoodsBrandDO::getId, GoodsBrandDO::getName)
                .eq(GoodsBrandDO::getCreateBy,userVo.getId());
        var brandDO = goodsBrandMapper.selectList(query);
        return success(CopyUtil.copyListNew(brandDO, PGoodsBrandSelectVm::new, null));
    }
}
